package com.sqx.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.RedisKeys;
import com.sqx.common.utils.Result;
import com.sqx.modules.course.dao.*;
import com.sqx.modules.course.entity.*;
import com.sqx.modules.course.service.AdsUnlockVideosService;
import com.sqx.modules.file.utils.DescribeException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class AdsUnlockVideosServiceImpl  extends ServiceImpl<AdsUnlockVideosDao, AdsUnlockVideos> implements AdsUnlockVideosService {
    @Autowired
    private AdsUnlockVideosDao adsUnlockVideosDao;
    @Autowired
    private AdsDao adsDao;
    @Autowired
    private CourseDetailsDao CourseDetailsDao;


    /**
     * 观看广告
     * @param userId 用户id
     * @param courseDetailsId  短剧剧集id
     * @param adsId 广告id
     */
    @Override
    public Result watchAds(Long userId, Long courseDetailsId, Long adsId) {
        CourseDetails courseDetails = CourseDetailsDao.selectById(courseDetailsId);
        if (ObjectUtils.isEmpty(courseDetails)) throw new DescribeException("剧集不存在",501);
        Ads ads = adsDao.selectById(adsId);
        if (ObjectUtils.isEmpty(ads))throw new DescribeException("广告不存在",501);
        //判断是否已经解锁
        LambdaQueryWrapper<AdsUnlockVideos> adsUnlockVideosLambdaQueryWrapper = new LambdaQueryWrapper<>();
        adsUnlockVideosLambdaQueryWrapper.eq(AdsUnlockVideos::getUserId,userId);
        adsUnlockVideosLambdaQueryWrapper.eq(AdsUnlockVideos::getCourseDetailsId,courseDetailsId);
        adsUnlockVideosLambdaQueryWrapper.eq(AdsUnlockVideos::getUnlockStatus,1);
        adsUnlockVideosLambdaQueryWrapper.eq(AdsUnlockVideos::getWatchStatus,1);
        AdsUnlockVideos videos = adsUnlockVideosDao.selectOne(adsUnlockVideosLambdaQueryWrapper);
        if(ObjectUtils.isNotEmpty(videos))throw new DescribeException("该剧集已经解锁",501);
        //看广告解锁剧集记录
        AdsUnlockVideos adsUnlockVideos = new AdsUnlockVideos();
        adsUnlockVideos.setCreateTime(new Date());
        adsUnlockVideos.setCourseDetailsId(courseDetailsId.intValue());
        adsUnlockVideos.setAdsId(adsId.intValue());
        adsUnlockVideos.setUserId(userId);
        adsUnlockVideosDao.insert(adsUnlockVideos);

        return Result.success().put("data",adsUnlockVideos);
    }

    /**
     * 接收第三方返回状态  解锁剧集
     * @param userId 用户id
     * @param unlockCourseId 短剧剧集id
     * @param status 解锁状态 0未解锁 1已解锁
     *
     */
    @Override
    public Boolean unlockCourseDetails(Long userId, Long unlockCourseId,Integer status) {
        AdsUnlockVideos videos = adsUnlockVideosDao.selectById(unlockCourseId);
        if (ObjectUtils.isEmpty(videos)) throw new DescribeException("解锁记录不存在",501);
        if (ObjectUtils.isEmpty(videos.getCourseDetailsId()))throw new DescribeException("剧集不存在",501);
        if (ObjectUtils.isEmpty(videos.getAdsId()))throw new DescribeException("广告不存在",501);
        if (ObjectUtils.isEmpty((videos.getUserId())))throw new DescribeException("用户不存在",501);
        if (ObjectUtils.isNotEmpty(videos)) {
            if (status==1){
                videos.setUnlockStatus(status);
                videos.setWatchStatus(1);
                adsUnlockVideosDao.updateById(videos);
                return true;
            }else {
                videos.setUnlockStatus(status);
                videos.setWatchStatus(0);
                adsUnlockVideosDao.updateById(videos);
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public Result selAdsUnlockVideosList(Long page, Long limit) {
        Page<AdsUnlockVideos> ipage =  new Page<>(page, limit);
        IPage<AdsUnlockVideos> adsUnlockVideosIPage = adsUnlockVideosDao.selectPage(ipage, null);
        return Result.success().put("data",adsUnlockVideosIPage);
    }

    @Override
    public Result selAdsUnlockVideosDetail(Long id) {
        AdsUnlockVideos adsUnlockVideos = adsUnlockVideosDao.selectById(id);
        return Result.success().put("data",adsUnlockVideos);
    }

    @Override
    public Result delAdsUnlockVideos(Long id) {
        adsUnlockVideosDao.deleteById(id);
        return Result.success();
    }

    @Override
    public Result addAdsUnlockVideos(AdsUnlockVideos adsUnlockVideos) {
        adsUnlockVideos.setCreateTime(new Date());
        adsUnlockVideosDao.insert(adsUnlockVideos);
        return Result.success();
    }

    @Override
    public Result updateAdsUnlockVideos(AdsUnlockVideos adsUnlockVideos) {
        adsUnlockVideos.setCreateTime(new Date());
        adsUnlockVideosDao.updateById(adsUnlockVideos);
        return Result.success();
    }

    @Override
    public Result selectAds(Long userId) {
        Ads ads = adsUnlockVideosDao.selectAds();
        if (ObjectUtils.isEmpty(ads)) throw new DescribeException("暂无广告",501);
        return Result.success().put("data",ads);
    }
    @Autowired
    private CourseDetailsDao courseDetailsDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private CourseCollectDao courseCollectDao;

    @Override
    public Result courseDetailsSelect(Long userId) {
        Page<CourseDetails> courseDetailsPage = new Page<>(1, 10);
        IPage<CourseDetails> courseDetailsIPage = courseDetailsDao.courseDetailsSelect(courseDetailsPage,userId);
        for (CourseDetails record : courseDetailsIPage.getRecords()) {
            Course course = courseDao.selectById(record.getCourseId());
            record.setCourse(course);
            LambdaQueryWrapper<CourseCollect> courseCollectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            courseCollectLambdaQueryWrapper.eq(CourseCollect::getUserId,userId);
            courseCollectLambdaQueryWrapper.eq(CourseCollect::getCourseDetailsId,record.getCourseDetailsId());
            courseCollectLambdaQueryWrapper.eq(CourseCollect::getClassify,1);
            courseCollectDao.selectOne(courseCollectLambdaQueryWrapper);
            if (ObjectUtils.isNotEmpty(courseCollectLambdaQueryWrapper)){
                record.setIsCollect(1);
            }else {
                record.setIsCollect(0);
            }
            LambdaQueryWrapper<CourseCollect> collectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            collectLambdaQueryWrapper.eq(CourseCollect::getUserId,userId);
            collectLambdaQueryWrapper.eq(CourseCollect::getCourseDetailsId,record.getCourseDetailsId());
            collectLambdaQueryWrapper.eq(CourseCollect::getClassify,2);
            courseCollectDao.selectOne(collectLambdaQueryWrapper);
            if (ObjectUtils.isNotEmpty(collectLambdaQueryWrapper)){
                record.setIsCollect(2);
            }
        }
        return Result.success().put("data",courseDetailsIPage);
    }
}
